<!--
 * @Author: xuzehan h935351619@163.com
 * @Date: 2024-07-22 14:37:57
 * @LastEditors: xuzehan h935351619@163.com
 * @LastEditTime: 2024-11-25 14:25:39
 * @FilePath: \linshi\src\views\EquipmentControl\FrequencyConverter.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="powerAmplifierBox">


    <a-card title="设备控制" style="width: 49%">
<!--      <a slot="extra" href="#" @click='handleSendPackage("deviceControl")'>配置</a>-->
       <a-form-model  :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
         <a-row>
           <a-col :span="20">
             <a-form-model-item label="控制模式">
               <a-select v-model="form.controlModel"  placeholder="请选择">
                 <a-select-option value="00">
                   本控
                 </a-select-option>
                 <a-select-option value="01">
                   远控
                 </a-select-option>
               </a-select>
             </a-form-model-item>
           </a-col>
           <a-col :span="4">
             <a-button type="primary" @click='controlModel()'> 确定 </a-button>
           </a-col>
         </a-row>

         <a-row>
           <a-col :span="20">
             <a-form-model-item label="射频开关">
               <a-select v-model="form.radioFrequencySwitch"  placeholder="请选择">
                 <a-select-option value="00">
                   关闭
                 </a-select-option>
                 <a-select-option value="01">
                   打开
                 </a-select-option>
               </a-select>
             </a-form-model-item>
           </a-col>
           <a-col :span="4">
             <a-button type="primary" @click='radioFrequencySwitch()' :disabled="form.controlModel !== '01'"> 确定 </a-button>
           </a-col>
         </a-row>

         <a-row>
           <a-col :span="20">
             <a-form-model-item label="衰减">
               <a-input-number :style="{width:'100%'}"  :min='0' :max='50' v-model="form.decay" placeholder="请输入0-50 单位dB" />
             </a-form-model-item>
           </a-col>
           <a-col :span="4">
             <a-button type="primary" @click='decay()' :disabled="form.controlModel !== '01'"> 确定 </a-button>
           </a-col>
         </a-row>

      </a-form-model>
    </a-card>
    <a-card title="网络设置" style="width:49%">
      <a slot="extra" href="#" @click='handleSendPackage("networkControl")'>配置</a>
       <a-form-model   ref="ruleForm" :rules="rules"  :model="formB" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="TCP端口">
          <a-input-number  :style="{width:'100%'}" :min='0' :max='65532' v-model="formB.tcpPort" placeholder="请输入0 ~ 65532" />
        </a-form-model-item>
        <a-form-model-item label="IP地址" prop='IPaddress'>
          <a-input v-model="formB.IPaddress" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="网关" prop='gateway'>
          <a-input v-model="formB.gateway" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="子网掩码" prop='subnetMask'>
          <a-input v-model="formB.subnetMask" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="MAC地址" prop='MACaddress'>
          <a-input v-model="formB.MACaddress" placeholder="请输入XX:XX:XX:XX:XX:XX(十六进制)" />
        </a-form-model-item>

      </a-form-model>
    </a-card>
  </div>
</template>

<script>
import { httpAction,getAction } from '@/api/manage'
import { intToHexString } from '@/utils/ByteUtil.js'
export default {
    data(){
      let validateIPaddress=(rule, value, callback)=> {
          const regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
          // return regex.test(value);
          console.log(regex.test(value),'校验')
           if (!regex.test(value)) {
               callback(new Error('ip地址格式错误'));
           }else{
              callback();
           }
        }
      let validateGateway=(rule, value, callback)=> {
          const regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
          // return regex.test(value);
           if (!regex.test(value)) {
               callback(new Error('网关地址格式错误'));
           }else{
              callback();
           }
        }
      let validateMACaddress=(rule, value, callback)=> {
          const regex = /^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/;
          // return regex.test(value);
           if (!regex.test(value)) {
               callback(new Error('MAC地址格式错误'));
           }else{
              callback();
           }
        }
      let validateSubnetMask=(rule, value, callback)=> {
          const regex = /^((255\.255\.255\.(0|128|192|224|240|248|252|254|255))|(255\.255\.(0|128|192|224|240|248|252|254|255)\.0)|(255\.(0|128|192|224|240|248|252|254|255)\.0\.0)|((0|128|192|224|240|248|252|254|255)\.0\.0\.0))$/;
          // return regex.test(value);
           if (!regex.test(value)) {
               callback(new Error('子网掩码格式错误'));
           }else{
              callback();
           }
        }
      return{
          labelCol: { span: 4 },
          wrapperCol: { span: 14 },
          form: {},
          formB:{},
          url:{
            sendPackage:"/channel/nmcChannelInfo/sendPackage",   
            getByChannelCode:'/attr/nmcAttr/getByChannelCode',
          },
          rules:{
            gateway: [{ validator: validateGateway, trigger: 'blur' }],
            IPaddress: [{ validator: validateIPaddress, trigger: 'blur' }],
            MACaddress: [{ validator: validateMACaddress, trigger: 'blur' }],
            subnetMask: [{ validator: validateSubnetMask, trigger: 'blur' }],
          },
      }
    },
    created() {
      this.paramInit()
    },
    methods:{
      paramInit() {
        let temp = {
          channelCode: '功放',
        }
        getAction(this.url.getByChannelCode, temp).then(res => {
          if (res.success) {
            res.result.forEach(i => {
              if (i.attrName === "ybk") {
                this.$set(this.form, 'controlModel' ,i.originalValue)
              } else if (i.attrName === "spkg") {
                this.$set(this.form, 'radioFrequencySwitch' ,i.originalValue)
              } else if (i.attrName === "sj") {
                this.$set(this.form, 'decay' ,i.parseValue)
              } else if (i.attrName === "ipdz") {
                this.$set(this.formB, 'IPaddress' ,i.parseValue)
              } else if (i.attrName === "wg") {
                this.$set(this.formB, 'gateway' ,i.parseValue)
              } else if (i.attrName === "zwym") {
                this.$set(this.formB, 'subnetMask' ,i.parseValue)
              } else if (i.attrName === "macdz") {
                this.$set(this.formB, 'MACaddress' ,i.parseValue)
              }else if (i.attrName === "tcpdk") {
                this.$set(this.formB, 'tcpPort' ,i.parseValue)
              }
            });
          }
        })
      },
      controlModel() {
        let data = {
          "channelCode": "功放",
          "nmcPackageGridValues": [
            {
              "gridCode": "826-参数编号",
              "packageGridValue": '01'
            },
            {
              "gridCode": "826-参数值",
              "packageGridValue": this.form.controlModel
            }
          ],
          "packageCode": "826-功放控制-设备控制"
        };
        httpAction(this.url.sendPackage,data,'post').then(res=>{
          if(res.success){
            this.$message.success(res.message)
          }else{
            this.$message.error(res.message)
          }
        });
      },
      radioFrequencySwitch() {
        let data = {
          "channelCode": "功放",
          "nmcPackageGridValues": [
            {
              "gridCode": "826-参数编号",
              "packageGridValue": '02'
            },
            {
              "gridCode": "826-参数值",
              "packageGridValue": this.form.radioFrequencySwitch
            }
          ],
          "packageCode": "826-功放控制-设备控制"
        };
        httpAction(this.url.sendPackage,data,'post').then(res=>{
          if(res.success){
            this.$message.success(res.message)
          }else{
            this.$message.error(res.message)
          }
        });
      },
      decay() {
        let data = {
          "channelCode": "功放",
          "nmcPackageGridValues": [
            {
              "gridCode": "826-参数编号",
              "packageGridValue": '03'
            },
            {
              "gridCode": "826-参数值",
              "packageGridValue": intToHexString(this.form.decay, 1)
            }
          ],
          "packageCode": "826-功放控制-设备控制"
        };
        httpAction(this.url.sendPackage,data,'post').then(res=>{
          if(res.success){
            this.$message.success(res.message)
          }else{
            this.$message.error(res.message)
          }
        });
      },
      handleSendPackage(val){
        if(val=='networkControl'){
          this.$refs.ruleForm.validate(valid => {
          if (valid) {
            let temp=this.ProcessParameters(val)
            httpAction(this.url.sendPackage,temp,'post').then(res=>{
              if(res.success){
                this.$message.success(res.message)
              }else{
                this.$message.error(res.message)
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
          });
        }else{
          let temp=this.ProcessParameters(val)
          httpAction(this.url.sendPackage,temp,'post').then(res=>{
            if(res.success){
              this.$message.success(res.message)
            }else{
              this.$message.error(res.message)
            }
          })
        }
      },
      handleFormat(val,symbol){
      //处理ip、网关、子网掩码、mac地址参数格式
      // IP[0]  IP[1]  IP[2]  IP[3]:
      // Eg: 192.168.1.11
      // 传输：0xC0  0xA8  0x01  0x0B
        let data=''
        let arr=val.split(symbol)
        console.log(val,symbol)
        console.log(arr)
        arr.forEach(i => {
         data+=Number(i).toString(16).toUpperCase().padStart(2,0)
        });
        return data
      },
      ProcessParameters(val){
        let data={}
        //网络配置
        if(val=='networkControl'){
          let {tcpPort,IPaddress,gateway,subnetMask,MACaddress}=this.formB
          data = {
                  "channelCode": "功放",
                  "channelId": "",
                  "nmcPackageGridValues":[
                  {
                    "gridCode": "828-攻放控制-网络控制-TCP端口",
                    "packageGridValue": tcpPort.toString(16).toUpperCase().padStart(4,0)
                  },
                  {
                    "gridCode": "828-攻放控制-网络控制-IP地址",
                    "packageGridValue":this.handleFormat(IPaddress,'.')
                  },
                  {
                    "gridCode": "828-攻放控制-网络控制-网关",
                    "packageGridValue":this.handleFormat( gateway,'.')
                  },
                  {
                    "gridCode": "828-攻放控制-网络控制-子网掩码",
                    "packageGridValue":this.handleFormat( subnetMask,'.')
                  },
                  {
                    "gridCode": "828-攻放控制-网络控制-MAC地址",
                    "packageGridValue":MACaddress.replace(/[:.：-]/g, '').toUpperCase()
                    // "packageGridValue":this.handleFormat( MACaddress,':')
                  },
                ],
                  "packageCode": "828-功放控制-网络控制"
                }
        }else if(val=='deviceControl'){
        //设备配置
        let {controlModel,radioFrequencySwitch,decay}=this.form
        data= {
                "channelCode": "功放",
                "channelId": "",
                "nmcPackageGridValues": [
                  {
                    "gridCode": "026-攻防控制-设备控制-控制模式",
                    "packageGridValue": controlModel
                  },
                  {
                    "gridCode": "026-攻防控制-设备控制-射频开关",
                    "packageGridValue": radioFrequencySwitch
                  },
                  {
                    "gridCode": "026-攻防控制-设备控制-衰减",
                    "packageGridValue": decay.toString(16).toUpperCase().padStart(2,0)
                  },
                ],
                "packageCode": "026-功放控制-设备控制"
              }

        }
       return data
      },

    },
}
</script>

<style lang='less' scoped>
.powerAmplifierBox{
  display:flex
}
</style>